//
//  CustomTabBar.swift
//  CustomTabBar
//
//  Created by mini on 2025/4/28.
//

import Foundation
import UIKit

class CustomTabBar: UITabBar {
    private var bgImageView: UIImageView?
    
    // 设置背景图
    func setBackgroundImage(_ image: UIImage?) {
        if bgImageView == nil {
            let imageView = UIImageView(frame: self.bounds)
            imageView.contentMode = .scaleAspectFill
            imageView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
            self.insertSubview(imageView, at: 0)
            bgImageView = imageView
        }
        bgImageView?.image = image
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        self.isTranslucent = true
        let customHeight: CGFloat = 120
        var tabFrame = self.frame
        tabFrame.size.height = customHeight
        tabFrame.origin.y = self.frame.origin.y + (self.frame.height - customHeight)
        self.frame = tabFrame

        // 保证背景图层大小同步
        bgImageView?.frame = self.bounds
    }
}
